//当前页数
var totalnum = 0;
//总页数
var totalPage = 0;


var j_Id = "0";     //员工编号
var uname = "0";    //员工姓名
var jcontent = "0"; //操作模块
var module_rname = "0"; //日志内容
var jdate1 = "0";  //记录时间开始
var jdate2 = "0";   //记录时间结束


/**
 * 周永成
 * 加载完发送ajax 渲染页面
 */
$(document).ready(function () {
    queryAlllogs(0);
});

function XuanRan(result) {

    totalPage = result.totalPage;
    var str = "";

    $(".t").find('*').remove();
    if (result.data.length === 0) {
        //渲染总页数(红色)
        $(".count").html(0).css("color","red");
        //渲染页数(红色)
        $(".total").html(1).css("color","red");
        $(".t").append("<td colspan='7' style='text-align: center'>没有该类型的数据</td>")
        return;
    }
    /**
     * 开始渲染
     */
    for (let i = 0; i < result.data.length; i++) {
        str += "<tr>"+
            "<td>" + result.data[i].rownum + "</td>"+
            "<td>" + result.data[i].j_Id + "</td>"+
            "<td>" + result.data[i].uname + "</td>"+
            "<td>" + result.data[i].module_rname + "</td>"+
            "<td>" + result.data[i].jcontent + "</td>"+
            "<td>" + result.data[i].jdate + "</td>"+
            "<td>" + "<a href='/system/getJournalById/"+ result.data[i].j_Id +"' class='tablelink'>查看日志信息</a></td>";
    }
    totalnum = result.pageNum;
    //渲染数据
    $(".t").html(str);
    str = "<li onclick='onclick_a(totalnum-1)' class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\"></span></a></li>";
    //默认第一页面
    if (totalnum === 0 || totalnum === 1){
        str += "<li onclick='onclick_a(1)' class=\"paginItem current\"><a href=\"javascript:;\">"+ 1 + "</a></li>"
    }else {
        str += "<li onclick='onclick_a(1)' class=\"paginItem\"><a href=\"javascript:;\">"+ 1 + "</a></li>"
    }
    //开始渲染分页按钮
    for (let i = 1; i <= result.totalPage; i++) {
        if (i <= 5 && i != 1) {
            if (i == totalnum) {
                str += "<li onclick='onclick_a("+ i + ")'  class=\"paginItem current\"><a href=\"javascript:;\">"+ i + "</a></li>"
            }else {
                str += "<li onclick='onclick_a("+ i + ")'  class=\"paginItem\"><a href=\"javascript:;\">"+ i + "</a></li>"
            }
        }else if (i === 6) {
            str += "<li onclick='onclick_a("+ i + ")'  class=\"paginItem more\"><a href=\"javascript:;\">...</a></li>"
        }else {
            continue;
        }
    }
    str +="<li onclick='onclick_a("+ (totalnum + 1) + ")'  class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\"></span></a></li>";
    //渲染总页数(红色)
    $(".count").html(result.totalRows).css("color","red");
    //渲染页数(红色)
    $(".total").html(result.pageNum).css("color","red");
    //渲染分页按钮
    $(".paginList").html(str);
}

/**
 * 渲染页面 (分页)
 * 周永成
 */
function queryAlllogs(num) {
    $.ajax({
        url:'/system/queryJournalByLike/'+j_Id+"/"+uname+"/"+jcontent+"/"+module_rname+"/"+jdate1+"/"+jdate2+"/"+num,
        type:'post',
        dataType:'json',
        success:function (result) {
            XuanRan(result)
        },
        error:function () {
            alert("no")
        }
    });
}

/**
 *  周永成
 *  点击按钮事件
 */
function onclick_a(index) {

    if (index <= 0 || index > totalPage){
        alert("已经是最后一页了")
    } else {
        var list = $(".paginList").find('li');

        for (var i = 0; i < list.length; i++) {
            $(list[i]).attr("class","paginItem");
        }

        $(list[index]).attr("class","paginItem current");
        queryAlllogs(index);
    }

}

/**
 * 周永成
 * 模糊查询
 */
function serach() {

    totalnum = 0;
    j_Id = $("input[name=j_Id]").val();
    j_Id = j_Id ==""?"0":j_Id;
    uname = $("input[name=uname]").val();
    uname = uname ==""?"0":uname;
    jcontent = $("input[name=jcontent]").val();
    jcontent = jcontent ==""?"0":jcontent;
    module_rname = $("input[name=module_rname]").val();
    module_rname = module_rname ==""?"0":module_rname;
    jdate1 = ($("input[name=jdate1]").val()).split(' ')[0];
    jdate1 = jdate1==""?"0":jdate1;
    jdate2 = ($("input[name=jdate2]").val()).split(' ')[0];
    jdate2 = jdate2==""?"0":jdate2;

    queryAlllogs(totalnum);
}